package cn.yuxiaoyuan.controller;

import cn.yuxiaoyuan.article.doamin.ArticleType;
import cn.yuxiaoyuan.article.query.ArticleTypeQuery;
import cn.yuxiaoyuan.common.util.R;
import cn.yuxiaoyuan.service.IArticleTypeService;
import cn.yuxiaoyuan.service.IUserService;
import cn.yuxiaoyuan.user.domain.User;
import cn.yuxiaoyuan.user.query.UserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/5/11 10:37
 */
@Api(value = "文章类型相关的接口")
@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    @GetMapping("/{id}")
    public R findById(@PathVariable("id")Long id){
        return R.ok(articleTypeService.findById(id));
    }

    @ApiOperation(value = "查询全部用户信息")
    @GetMapping
    public R findAll(){
        return R.ok(articleTypeService.findAll());
    }

    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id")Long id){
        articleTypeService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    public R batchDelete(@RequestBody List<Long> ids){
        articleTypeService.batchDelete(ids);
        return R.ok();
    }

    @PostMapping
    public R add(@RequestBody ArticleType articleType){
        if(articleType.getId() == null){
            articleType.setCreateTime(new Date());
            articleTypeService.add(articleType);
        }else {
            if(articleType.getId() != articleType.getParentId()){
                articleType.setUpdateTime(new Date());
                articleTypeService.update(articleType);
            }else {
               return R.fail("不能选择自己做为父类");
            }
        }
        return R.ok();
    }

    @PostMapping("/list")
    public R pageList(@RequestBody ArticleTypeQuery articleTypeQuery){
        return articleTypeService.pageList(articleTypeQuery);
    }

    @GetMapping("/getTree")
    public R getTree(){
        return articleTypeService.getTree();
    }
}
